<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" src="jquery-3.6.0.min.js"></script>
</head>

<body>
    <form class="">
        <div class="row">
            <div class="col el-row">
                <banner></banner>
            </div>
        </div>
        <div id="iframeEditor_div" class="row" style="margin:0px">
            <div id="iframeEditor"></div>
        </div>
    </form>
    <script src="http://localhost:85/web-apps/apps/api/documents/api.js"></script>
    <script type="text/javascript">
        var api = "http://localhost:850/api/doc/fileversion/";
        var docEditor;
        var innerAlert = function (message) {
            if (console && console.log) console.log(message);
        };

        var onAppReady = function () {
            innerAlert("Document editor ready");
        };

        var onDocumentStateChange = function (event) {
            var title = document.title.replace(/\*$/g, "");
            document.title = title + (event.data ? "*" : "");
        };

        var onRequestEditRights = function () {
            location.href = location.href.replace(RegExp("action=view\\&?", "i"), "");
        };

        var onError = function (event) {
            if (event) innerAlert(event.data);
        };
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }
        var onOutdatedVersion = function (event) {
            location.reload(true);
            console.log(event);
        };
        window.onload = function () {
            var fileId = getUrlParam("fileid");
            var ctype = getUrlParam("ctype");
            var systemType = getUrlParam("systemType");
            let url =
                api +
                "config?editType=" +
                ctype +
                "&systemType=" +
                systemType +
                "&fileid=" +
                fileId;
            $.ajax({
                type: "post",
                url: url,
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                beforeSend: function (request) {
                    request.setRequestHeader(
                        "Authorization",
                        "Bearer " + window.localStorage.getItem("gloabl_token"),
                        "x-api-version",
                        1.0
                    );
                    request.setRequestHeader("x-api-version", 1.0);
                },
                success: function (data) {
                    var config = data.body;
                    if (data.status) {
                        config.events = {
                            onAppReady: onAppReady,
                            onDocumentStateChange: onDocumentStateChange,
                            onRequestEditRights: onRequestEditRights,
                            onError: onError,
                            onOutdatedVersion: onOutdatedVersion,
                        };
                        config.events["onRequestHistoryClose "] = function () {
                            document.location.reload();
                        };
                        config.events["onRequestHistory"] = function () {
                            LoadHist(fileId);
                        };
                        config.events["onRequestHistoryData"] = function (event) {
                            var ver = event.data;
                            setHist(ver, fileId);
                        };
                        docEditor = new DocsAPI.DocEditor("iframeEditor", config);
                    } else {
                        alert(data.message)
                    }
                },
            });
        };
        function LoadHist(fileId) {
            let url = api + "hist?fileid=" + fileId;
            $.ajax({
                type: "post",
                url: url,
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                beforeSend: function (request) {
                    request.setRequestHeader(
                        "Authorization",
                        "Bearer " + window.localStorage.getItem("gloabl_token"),
                        "x-api-version",
                        1.0
                    );
                    request.setRequestHeader("x-api-version", 1.0);
                },
                success: function (data) {
                    if (data.status) {
                        var hit = data.body;
                        docEditor.refreshHistory(hit);
                    } else {
                        alert(data.message)
                    }
                },
            });
        }
        function setHist(version, fileid) {
            let url =
                api +
                "sethist?fileid=" +
                fileid +
                "&version=" +
                version;
            $.ajax({
                type: "post",
                url: url,
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                beforeSend: function (request) {
                    request.setRequestHeader(
                        "Authorization",
                        "Bearer " + window.localStorage.getItem("gloabl_token"),
                        "x-api-version",
                        1.0
                    );
                    request.setRequestHeader("x-api-version", 1.0);
                },
                success: function (data) {
                    if (data.status) {
                        var hit = data.body;
                        docEditor.setHistoryData(hit);
                    } else {
                        alert(data.message)
                    }
                },
            });
        }
        $(function () {
            $("#iframeEditor_div").height($(window).height() - 10);
        });
    </script>
</body>

</html>